//
//  LLProgressHUD.m
//  ChatTool
//
//  Created by my_liu on 2016/11/5.
//  Copyright © 2016年 storemax. All rights reserved.
//

#import "LLProgressHUD.h"

@implementation LLProgressHUD
+ (LLProgressHUD *)shareLLMBProgressHUD
{
    static LLProgressHUD *hud = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hud = [[LLProgressHUD alloc] init];
    });
    return hud;
}

- (void)showHudOnView:(UIView*)aView WithTitle:(NSString*)aTitle WithMode:(MBProgressHUDMode)mode
{
    self.hud = [self constructorHUDOnView:aView WithTitle:aTitle WithMode:mode];
    [self.hud show:YES];
}

- (void)hideAfterTimeIntervals:(NSTimeInterval)timeDelay
{
    if (self.hud) {
        [self.hud hide:YES afterDelay:timeDelay];
    }
    if (self.hud.hidden) {
        [self.hud removeFromSuperview];
    }
}

- (void)hidenImmediatelyHud
{
    if (self.hud) {
        [self.hud hide:YES];
    }
}

- (void)showHudOnView:(UIView*)aView text:(NSString *)text progressHUDStatus:(LLProgressHUDStatus)status
{
    [self.hud removeFromSuperview];
    self.hud = [[LLMBProgressHUD alloc]initWithView:aView];
    [aView addSubview:self.hud];
    self.hud.mode = MBProgressHUDModeCustomView;
    [self.hud setMinSize:CGSizeMake(23, 23)];
    self.hud.detailsLabelText = text ? text : @"";
    self.hud.detailsLabelFont = [UIFont systemFontOfSize:16.0];
    self.hud.animationType = MBProgressHUDAnimationZoomOut;
    // y 轴偏移距离
    self.hud.yOffset = offsetForDirectionY;
    [self.hud show:YES];
    
    switch (status) {
        case LLProgressHUDStatusFailure:
        {
            self.hud.mode = MBProgressHUDModeCustomView;
            UIImageView *errorView = [[UIImageView alloc] initWithImage:[self bundleImage:@"MBPUD_hud_error"]];
            self.hud.customView = errorView;
        }
            break;
        case LLProgressHUDStatusSuccess:
        {
            self.hud.mode = MBProgressHUDModeCustomView;
            UIImageView *successView = [[UIImageView alloc] initWithImage:[self bundleImage:@"MBPUD_hud_success"]];
            self.hud.customView = successView;
        }
            break;
        case LLProgressHUDStatusInfo:
        {
            self.hud.mode = MBProgressHUDModeCustomView;
            UIImageView *infoView = [[UIImageView alloc] initWithImage:[self bundleImage:@"MBPUD_hud_info"]];
            self.hud.customView = infoView;
        }
            break;
        case LLProgressHUDStatusLoading:
        {
            self.hud.mode = MBProgressHUDModeIndeterminate;
        }
            break;
    }
    [self hideAfterTimeIntervals:1.5];
}

- (void)showHudOnView:(UIView*)aView WithTitle:(NSString*)aTitle WithMode:(MBProgressHUDMode)mode onQueue:(dispatch_queue_t)queue completionBlock:(LLProgressHUDCompletionBlock)completion
{
    self.hud = [self constructorHUDOnView:aView WithTitle:aTitle WithMode:mode];
    if (!queue) {
        queue = dispatch_get_main_queue();
    }
    [self.hud showAnimated:YES whileExecutingBlock:^{
        [NSThread sleepForTimeInterval:sleepForTimeInterval];
    } onQueue:queue completionBlock:^{
        completion();
    }];
}

- (LLMBProgressHUD *)constructorHUDOnView:(UIView*)aView WithTitle:(NSString*)aTitle WithMode:(MBProgressHUDMode)mode
{
    if (self.hud) {
        [self.hud removeFromSuperview];
    }
    self.hud = [[LLMBProgressHUD alloc]initWithView:aView];
    self.hud.yOffset = -20.0f;
    [aView addSubview:self.hud];
    self.hud.minSize = CGSizeMake(45.f, 45.f);
    if (!mode) {
        self.hud.mode = MBProgressHUDModeIndeterminate;
    } else {
        self.hud.mode = mode;
        self.hud.minSize = CGSizeMake(65.f, 15.f);
    }
    if (aTitle) {
        self.hud.detailsLabelText = aTitle;
    } else {
        self.hud.detailsLabelText = @"";
    }
    self.hud.detailsLabelFont = [UIFont systemFontOfSize:16.0];
    
    return self.hud;
}

- (UIImage *)bundleImage:(NSString*)imageName
{
    NSString *bunldPath = [[NSBundle mainBundle]pathForResource:@"LLProgressHUDResourceBundle" ofType:@"bundle"];
    NSString *imagePath = [bunldPath stringByAppendingPathComponent:imageName];
    
    return [UIImage imageWithContentsOfFile:imagePath];
}

@end
